package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<SetmealDto>> page(int page, int pageSize, String name){
        //分类的分页查询
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(name),Setmeal::getName,name);
        lqw.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(setmealPage,lqw);

        //创建Page<SetmealDto>分页对象
        Page<SetmealDto> dtoPage = new Page<>();
        //复制分页数据
        BeanUtils.copyProperties(setmealPage,dtoPage,"records");

        //处理分页集合数据
        List<Setmeal> records = setmealPage.getRecords();
        ArrayList<SetmealDto> dtoList = new ArrayList<>();

        //遍历
        records.forEach(setmeal -> {
            //创建Setmeal对象
            SetmealDto dto = new SetmealDto();
            //复制setmeal属性-->dto中
            BeanUtils.copyProperties(setmeal,dto);
            //查询分类信息
            Category category = categoryService.getById(setmeal.getCategoryId());
            if (category != null){
                dto.setCategoryName(category.getName());
            }
            dtoList.add(dto);
        });

        //将处理后集合，添加到分页对象中
        dtoPage.setRecords(dtoList);
        return R.success(dtoPage);

    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        setmealService.removeByDish(ids);
        return R.success("删除套餐成功");
    }

    /**
     * 根据id查询套餐信息和对应的菜品信息(回显)
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Setmeal> get(@PathVariable Long id){
        SetmealDto setmealDto = setmealService.getByIdWithFlavor(id);
        return R.success(setmealDto);
    }

    /**
     * 修改套餐信息，同时修改对应的菜品信息
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        setmealService.updateWithFlavor(setmealDto);
        return R.success("修改菜品成功");
    }

    /**
     * 修改status状态
     * @param type
     * @param ids
     * @return
     */
    @PostMapping("/status/{type}")
    public R<String> updateStatus(@PathVariable Integer type,Long[] ids){
        for (Long id : ids) {
            Setmeal setmeal = setmealService.getById(id);
            if (type == 0){
                setmeal.setStatus(0);
            }else {
                setmeal.setStatus(1);

            }
            setmealService.updateById(setmeal);
        }
        return R.success("修改状态成功");
    }

    /**
     * 移动端查询套餐集合
     * @param setmeal
     * @return
     */
    @GetMapping("list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        //条件构造器
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        lqw.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        lqw.orderByDesc(Setmeal::getUpdateTime);

        //查询
        List<Setmeal> list = setmealService.list(lqw);
        return R.success(list);
    }
}
